package com.linux.huhx.firstdemo;


import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.concurrent.TimeUnit;

/**
 * @Author: huhx
 * @Date: 2017-11-03 上午 11:24
 */
public class HuhxConsumer {
    public static void main(String[] args) throws JMSException, InterruptedException {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
        // JMS clients use the ConnectionFactory object to create connections to a JMS provider.
        Connection connection = connectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 会话
        Destination destination = session.createQueue("HuhxSend"); // 队列

        MessageConsumer consumer = session.createConsumer(destination);
        while (true) {
            ObjectMessage message = (ObjectMessage) consumer.receive();
            if (message != null) {
                MailMessage mailMessage = (MailMessage) message.getObject();
                System.out.println("send message " + mailMessage);
                TimeUnit.SECONDS.sleep(2);
                System.out.println("End");
            }
        }
    }
}
